﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PerformanceCounterTester
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        PerformanceCounter counter;
        ObservableCollection<CounterViewModel> _counterViewModels = new ObservableCollection<CounterViewModel>();

        public MainWindow()
        {
            InitializeComponent();
            counter = null;
            lvwCounters.ItemsSource = _counterViewModels;
            cboCounterType.ItemsSource = Enum.GetValues(typeof(PerformanceCounterType));
        }

        private void BtnGrab_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                counter = new PerformanceCounter(txtCategory.Text, txtCounter.Text, false);

                if (counter == null)
                {

                    BtnIncrement.IsEnabled = false;
                    btnDecrement.IsEnabled = false;
                    refresh.IsEnabled = false;
                }
                else
                {
                    BtnIncrement.IsEnabled = true;
                    btnDecrement.IsEnabled = true;
                    refresh.IsEnabled = true;
                    counter.RawValue = 0;

                    currentValue.Content = counter.NextValue().ToString("F");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void BtnIncrement_Click(object sender, RoutedEventArgs e)
        {
            counter.Increment();
        }

        private void refresh_Click(object sender, RoutedEventArgs e)
        {
            currentValue.Content = counter.NextSample().BaseValue.ToString();
        }

        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CounterCreationDataCollection dataCollection = new CounterCreationDataCollection();

                foreach (CounterViewModel model in _counterViewModels)
                {
                    CounterCreationData creationData = new CounterCreationData(model.CounterName, 
                         model.CounterName, model.Category);
                    dataCollection.Add(creationData);
                }
                PerformanceCounterCategory.Create(txtCounterCategoryName.Text, txtCounterCategoryName.Text,
                    PerformanceCounterCategoryType.SingleInstance
                    ,dataCollection);

            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                PerformanceCounterCategory.Delete(txtCounterCategoryName.Text);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnCreateCounter_Click(object sender, RoutedEventArgs e)
        {
            if (txtCounterName.Text.Length > 0 && cboCounterType.SelectedItem !=null)
            {
                _counterViewModels.Add(new CounterViewModel()
                {
                    CounterName = txtCounterName.Text,
                    Category = (PerformanceCounterType)cboCounterType.SelectedValue
                });

                
            }
        }

        private void btnDecrement_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
